// MUD Programming
// Ron Penton
// (C)2003
// BasicLibString.cpp - Auxilliary string functions not provided by the standard
//                      library.


#include "BasicLibString.h"
#include <cctype>
#include <stdlib.h>
#include <string>
namespace BasicLib
{

/**
 * [TrimWhitespace description]
 * @param  p_string [target string]
 * @return          [the string which be removed whitespace at begin and end]
 */
std::string TrimWhitespace( const std::string& p_string )
{
    int wsf;
    int wsb;

    // trim the front
    wsf = p_string.find_first_not_of( WHITESPACE );
    wsb = p_string.find_last_not_of( WHITESPACE );

    if( wsf == std::string::npos )
    {
        wsf = 0;
        wsb = -1;
    }

    return p_string.substr( wsf, wsb - wsf + 1 );
}

/**
 * [ParseWord description]
 * @param  p_string [target string]
 * @param  p_index  [index of word]
 * @return          [the word at p_index postion]
 */
std::string ParseWord( const std::string& p_string, int p_index )
{
    int wss = p_string.find_first_not_of( WHITESPACE );

    while( p_index > 0 )
    {
        p_index--;

        // find the beginning of the next word, by finding whitespace
        // to end the current word, and then non-whitespace at the start
        // of the next word
        wss = p_string.find_first_of( WHITESPACE, wss );
        wss = p_string.find_first_not_of( WHITESPACE, wss );
    }

    // find the end of the word
    int wse = p_string.find_first_of( WHITESPACE, wss );

    if( wss == std::string::npos )
    {
        wss = 0;
        wse = 0;
    }

    return p_string.substr( wss, wse - wss );
}

/**
 * [RemoveWord description]
 * @param  p_string [target string]
 * @param  p_index  [index of word want to remove]
 * @return          [the string that the word has be removed ]
 */
std::string RemoveWord( const std::string& p_string, int p_index )
{
    int wss = p_string.find_first_not_of( WHITESPACE );

    while( p_index > 0 )
    {
        p_index--;

        // find the beginning of the next word, by finding whitespace
        // to end the current word, and then non-whitespace at the start
        // of the next word
        wss = p_string.find_first_of( WHITESPACE, wss );
        wss = p_string.find_first_not_of( WHITESPACE, wss );
    }

    // find the end of the word
    int wse = p_string.find_first_of( WHITESPACE, wss );

    // find the beginning of the next word
    wse = p_string.find_first_not_of( WHITESPACE, wse );

    if( wss == std::string::npos )
    {
        wss = 0;
        wse = 0;
    }

    std::string str = p_string;
    str.erase( wss, wse - wss );
    return str;
}



} // end namespace BasicLib


